// import timeFormat from './timeFormat'
let timeFormat = uni.$u.timeFormat;
class timefn {
  constructor() {}
  gettime(num = 0, date = new Date(), format = "yyyy-mm-dd") {
    let dateTime = Number(new Date(date));
    dateTime = dateTime - Number(num) * 24 * 60 * 60 * 1000;
    return timeFormat(dateTime, format);
  }
  getdateTime(date) {
    if (!date) {
      return new Date().getTime();
    }
    return new Date(Date.parse(date.replace(/-/g, "/"))).getTime();
  }
  getUTCtime(dateTime1, dateTime2, r = 60) {
    let dateTime2num = this.getdateTime(dateTime2);
    let dateTime1num = this.getdateTime(dateTime1);

    let timer = Math.floor(
      (Number(dateTime2num) - Number(dateTime1num)) / 1000 / r
    );
    return timer;
  }
  getUTCtimeceil(dateTime1, dateTime2, r = 60) {
    let dateTime1num = this.getdateTime(dateTime1);
    let dateTime2num = this.getdateTime(dateTime2);

    let timer = Math.ceil(
      (Number(dateTime2num) - Number(dateTime1num)) / 1000 / r
    );
    return timer;
  }
  secondToTimeStr(t) {
    if (!t) return "00:00:00";
    if (t < 60) return "00:" + ((i = t) < 10 ? "0" + i : i);
    if (t < 3600)
      return (
        "" +
        ((a = parseInt(t / 60)) < 10 ? "0" + a : a) +
        ":" +
        ((i = t % 60) < 10 ? "0" + i : i)
      );
    if (3600 <= t) {
      var a,
        i,
        e = parseInt(t / 3600);
      return (
        (e < 10 ? "0" + e : e) +
        ":" +
        ((a = parseInt((t % 3600) / 60)) < 10 ? "0" + a : a) +
        ":" +
        ((i = t % 60) < 10 ? "0" + i : i)
      );
    }
  }
  secondToTimeStrh(t) {
    if (!t) return;
    if (t < 60) return `00时00分`;
    if (t < 3600)
      return "00时" + ((a = parseInt(t / 60)) < 10 ? "0" + a : a) + "分";
    if (3600 <= t) {
      var a,
        i,
        e = parseInt(t / 3600);
      return (
        (e < 10 ? "0" + e : e) +
        "时" +
        ((a = parseInt((t % 3600) / 60)) < 10 ? "0" + a : a) +
        "分"
      );
    }
  }
  gettimemm(num = 0, format = "yyyy-mm-dd") {
    let dateTime = new Date();
    dateTime.setDate(1);
    dateTime.setMonth(dateTime.getMonth() - num);
    return timeFormat(new Date(dateTime), format);
  }
  gettimemmt(num = 0, format = "yyyy-mm-dd") {
    let dateTime = new Date();
    // dateTime.setDate(1)
    dateTime.setMonth(dateTime.getMonth() - num);
    return timeFormat(new Date(dateTime), format);
  }
  gettimemmt2(date, num = 0, format = "yyyy-mm-dd") {
    let dateTime = new Date(this.getdateTime(date));

    // dateTime.setDate(1)
    dateTime.setMonth(dateTime.getMonth() + num);
    return timeFormat(new Date(dateTime), format);
  }
  gettimeyyt(date, num = 0, format = "yyyy-mm-dd") {
    let dateTime = new Date(this.getdateTime(date));

    // dateTime.setDate(1)
    dateTime.setUTCFullYear(dateTime.getFullYear() + num);
    return timeFormat(new Date(dateTime), format);
  }
}

// const now = new Date();
// const deadline = new Date("2023-06-30");
// console.log(getMonthDistance(deadline));
// let time = new timefn()
// console.log(time.gettimemm(100))
export default new timefn();
